/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import ts from '../../vendor/ohos-typescript';

export function isDeclaredNamespace(node: ts.Node): boolean {
  let current: ts.Node | undefined = node;
  while (current) {
    if (
      ts.isModuleDeclaration(current) &&
      current.modifiers?.some(mod => mod.kind === ts.SyntaxKind.DeclareKeyword)
    ) {
      return true;
    }
    if (
      ts.isVariableStatement(current) &&
      current.modifiers?.some(mod => mod.kind === ts.SyntaxKind.DeclareKeyword)
    ) {
      return true;
    }
    current = current.parent;
  }
  return false;
}

export function shouldReportVariableDeclaration(node: ts.VariableDeclaration | ts.PropertyDeclaration): boolean {
    const { type, initializer } = node;
    if (!type || !initializer) { return false };
    // 跳过基本类型注解
    if (!ts.isLiteralTypeNode(type) &&
        (type.kind === ts.SyntaxKind.StringKeyword ||
            type.kind === ts.SyntaxKind.NumberKeyword)) {
        return false;
    }
    // 跳过函数类型
    if (ts.isFunctionTypeNode(type) || ts.isMethodDeclaration(node)) {
        return false;
    }
    // 跳过模板字面量
    if (ts.isTemplateLiteral(initializer)) {
        return false;
    }
    // 检查字面量类型注解
    if (ts.isLiteralTypeNode(type)) {
        if (ts.isStringLiteral(initializer) && ts.isStringLiteral(type.literal)) {
            return initializer.text === type.literal.text;
        }
        if (ts.isNumericLiteral(initializer) && ts.isNumericLiteral(type.literal)) {
            return initializer.text === type.literal.text;
        }
    }
    return false;
};

export function processModuleDeclaration(node: ts.ModuleDeclaration, moduleBlock: boolean): boolean {
    const isInternalModule = ts.isIdentifier(node.name); // 内部模块名称是标识符
    const isExternalModule = ts.isStringLiteral(node.name); // 外部模块名称是字符串字面量
    if (isExternalModule) {// 外部模块声明，不需要处理
        return false;
    };
    if (isInternalModule) {
        let text = node.getText();
        if (moduleBlock) {
            text = node.getFullText();
        };
        if (ts.isModuleDeclaration(node) || moduleBlock) {
            const moduleTokens = Array.from(node.getChildren());
            const moduleKeywordNode = moduleTokens.find(token =>
                token.kind === ts.SyntaxKind.ModuleKeyword
            );
            if (moduleKeywordNode && moduleKeywordNode.getText() === 'module') {
                // 创建缺陷报告和修复建议
                return true;
            };
        };
    };
    // 递归处理嵌套的模块声明
    if (node.body && ts.isModuleBlock(node.body)) {
        for (const statement of node.body.statements) {
            if (!ts.isModuleDeclaration(statement)) {
                continue;
            };
            if (processModuleDeclaration(statement as ts.ModuleDeclaration, true)) {
                return true;
            }
        };
    };
    return false;
};

